﻿#pragma once

#include "Hash.hpp"

template<class K, class V, class HashFunc = DefaultHash<K>>
class unordered_map
{
	struct MapKeyOfT
	{
		const K& operator()(const pair<K, V>& kv)
		{
			return kv.first;
		}
	};
public:
	typedef typename HashTable<K, pair<K, V>, MapKeyOfT, HashFunc>::iterator iterator;

	iterator begin()
	{
		return _ht.begin();
	}

	iterator end()
	{
		return _ht.end();
	}

	pair<iterator, bool> insert(const pair<K, V>& kv)
	{
		return _ht.insert(kv);
	}

	iterator find(const K& key)
	{
		return _ht.find(key);
	}

	bool erase(const K& key)
	{
		return _ht.erase(key);
	}

	V& operator[](const K& key)
	{
		pair<iterator, bool> ret = insert(make_pair(key, V()));
		return ret.first->second;
	}

private:
	HashTable<K, pair<K, V>, MapKeyOfT, HashFunc> _ht;
};


void test_map()
{
	unordered_map<string, string> dict;
	dict.insert(make_pair("sort", "排序"));
	dict.insert(make_pair("left", "剩余"));
	dict.insert(make_pair("left", "左边"));
	dict["string"];
	dict["left"] = "左边";
	dict["string"] = "字符串";

	unordered_map<string, string>::iterator it = dict.begin();
	
	while (it != dict.end())
	{
		cout << it->first << " " << it->second << endl;
		++it;
	}

	cout << endl;

	for (auto& kv : dict)
	{
		cout << kv.first << " " << kv.second << endl;
	}
}